package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102600762815";

    private String timeout = "30m";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCP7besc+V3WIzT3gDEjMDPlI9D8tU22jTfxelpjnG/2XPplo5J0LJLs/ruBanYWpxQx8FZWfhzCd9IHSYpnY9E/j4hJX7oNmRSzGSmxhm459AYlZXwbEKDNM9tpI233buxivXzQSP4l2dkm6hwlenQt1TUYJXDa0rtVjOqN3qqcBwLDwIye1MOP7Lx+ZM7jA8xngo28jnxzzfqHH1CMaeFEOtxGLUCGz2YSh6SA9OyFTK57jhfYh1f6U6Gxcn4p0pF5ra1POiOnFVjYCMoOrysBAYjKfsGDiVTNg13+W2hP9r+jSdVisieskKnGqh73dATCQPUBfjutkZHHvNmvsFPAgMBAAECggEAXsZHQ1zRxkPeAAlY8hRJRqdpq3G06rjHRm8ZYzO0PYOgazmDmqG9NlJesuuxytDny92tuPuguYfPqrnfDlo9JE5SdRsutRRT/Z2vzY33FCY5hmbB5B41shZNXMKVu3RcCu4ZRMQpPqHcKIjDYtekrpZ6Wpr7I0J8hUpVHQoJUT8in6adurzm+AHRyshZPlzJ+o8AZkjPcbWlmvWLTWvZN2acoyIm5cACu0F6Ti0G+FRFBROYD6P1cfSSpDMO5IgBXHe+R9ghWehK5H0GNAMfM0lMj5vgsTJdcznLiYjCoQjawGxEjPo5fbm5nV2Gdj2hVxuXuK/yziKV2/NH1VclSQKBgQDFsiGDPeDXdK2uc6sufgHU39DUrkejhfR8/BtUYlcE3aZvztT+RME0XXoSkPQh8nNoY/EOGn5AUQvM2PAcHV8mJ9aJ44SQR0iONY7Vpp32pdBwX70oNzwNHM5gBQhyNC+SIUTbE5XqJ5CO5mXVmBDjumAjTFFE+HQDy7bJe5YlPQKBgQC6YDR6LA8XiaTY5Q+yasZoMMEDUR9wltT8baymO1yfLqUuT3QdGsnxPqG4QbmnelyDX75ccQNSKg1PUp/ZxLj89/p7nKSjITQnuaxjm+5kYR0uK+wl/MhEZbNBA5sXGDa+ZUINMSVxuWdic+WIiER+RAvHNEvnSr3WXD6lbuYhewKBgQCWpMUar8UsCLHsFCHl/IjCwIXt+puGRKXns2fT5leu0oNoAXCWDKg4kd5aVXBiidBdRILEH3YC5n8aaliU4+DEP2kgi2lRU7A7BR7BOQR3lSR+7MVD5srwpvmve0ZTV+bD8Gv6kDMVSW7DJVeF6ik8jqGBZD8tUjF9TvcLEwx3dQKBgAv+DsJhvWLdV38hVh474qJnqLPG74f2HKRVhkt3/GC9xqExOZ55z863If7SM9kD9Zn6eixGlVkfHZYiEekYUHLune6ASNO6WjnIJ8CNEwzgdtk2uE5Fcrl79cnUKpGU3EjqCRMCO4+Sx4U9QeLEKewaAswBoHS0iXyWmO1aMnKrAoGAbAmOHK7YenujMzkTeq07b+5/Z4w/UmY+HQ6FDDhd0TMFmPy5MHx9eMIkkA+As6sSLJc0BFi34SgN8Q5EYMtlkbozzDGSDhPxjFfa77YLioycUtbPg3waVF5zEilmuezt2mYBVWaFspomRac6erSaIfEPSxVIPY7adb3kGgJduvE=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApQzsqR1dZmtbppB4CZtcN99wyqBiPAOB9H8hGIO87Hwm+1koUZDetRQ89IJw9Mz5sgWdlmudpOLGnVEbGIDegjJ8qoZl42+4AhnOq5QvE6d+wtiKs+yoziIFMdTBvYsEpYqeVBfpMbh115YE7pE9w+La0RN2CX2z80F53+yl7x9QkZvneMbok8uuba6T2/HZ7nqWmep7+vjDrxN+V0StKDsbIoCqa93n8r9PHQP04cx1JTzz1Pa2VfkRtgfM4ZdjuEqLmzyNymIRv6QH2UoCvklDm1AA8Wbx8GDZ3r96SXxLdg1APtFNmPiCX1Uugpxr+qSOzuTWw2ZrRY3kSHEfowIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    //TODO 要配置的回调地址
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    //TODO 要配置的回调地址AlipayTemplate
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","//设置订单超时时间
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}